local Class = require('lua.lang.Class')
local InputStream = require('lua.io.InputStream')

--- @class ByteArrayInputStream : InputStream
--- @field bytes number[]
--- @field pos number
--- @field markPos number
local ByteArrayInputStream = Class {
	__extends = InputStream
}

--- @constructor
--- @param bytes number[]
--- @return ByteArrayInputStream
function ByteArrayInputStream:new(bytes)
	--- @type ByteArrayInputStream
	local o = {}
	o.bytes = bytes
	o.pos = 1
	o.markPos = o.pos
	return setmetatable(o, ByteArrayInputStream)
end

--- @return number
function ByteArrayInputStream:read()
	local pos = self.pos
	local b = self.bytes[pos] or -1
	if b == -1 then
		return b
	end
	self.pos = pos + 1
	return b
end

--- @return void
function ByteArrayInputStream:close()
	self.bytes = nil
	self.pos = nil
end

--- @param readAheadLimit number
--- @return void
function ByteArrayInputStream:mark(readAheadLimit)
	self.markPos = self.pos
end

--- @return void
function ByteArrayInputStream:reset()
	self.pos = self.markPos
end

--- @return boolean
function ByteArrayInputStream:markSupported()
	return true
end

return ByteArrayInputStream